package cn.com.xinjinfu.pdoulcProduct.WebView;

import android.app.Activity;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;
import cn.com.xinjinfu.pdoulcProduct.view.ProgressWebView;


/**
 * @Description:WebView界面，带自定义进度条显示
 * @author http://blog.csdn.net/finddreams
 */ 
public class YaoqingWeb extends Activity {
	UMImage image = new UMImage(this, R.drawable.icon_pdou180);
	String appUrl = DataHandler.WEBURL+"/front/wa/appShare";
	protected ProgressWebView mWebView;
	private String url;
	private LoadStatusBox loadbox;
	private LinearLayout ll_top_left;
	private TextView top_title;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yaoqing_web);
		ll_top_left = (LinearLayout) findViewById(R.id.ll_top_left);
		top_title = (TextView) findViewById(R.id.top_title);
		mWebView = (ProgressWebView) findViewById(R.id.baseweb_webview);
		User user = AccountManager.getInstance().getUser();
		if (user != null && user.getCurrUser().isLogged()) {
			appUrl = appUrl + "?code="+user.getShareCode();
		}
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		url = bundle.getString("url");
		WebChromeClient wvcc = new WebChromeClient() {
			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				Log.d("ANDROID_LAB", "TITLE=" + title);
				TitleManager.showTitle(YaoqingWeb.this, title, "");
			}

		};
		// 设置setWebChromeClient对象
		mWebView.setWebChromeClient(wvcc);
		loadbox = (LoadStatusBox) this.findViewById(R.id.loadStatusBox);
		loadbox.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				netCheck();
			}
		});
		loadbox.loading();
		netCheck();

		ll_top_left.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (mWebView.canGoBack()) {
					mWebView.goBack();// 返回前一个页面
				}else{
					YaoqingWeb.this.finish();
				}
			}
		});
	}

	private void netCheck(){
		if (!DataHandler.isNetworkConnected(this)) {
			loadbox.failed();
			ToastManager.show(this, "请检查网络连接");
			return;
		}else {
			initData();
			loadbox.success();
		}
	}

	protected void initData() {
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				//handler.cancel(); // Android默认的处理方式
				handler.proceed();  // 接受所有网站的证书
				//handleMessage(Message msg); // 进行其他处理
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				if (url != null) {
					Log.i("url", url);
					if (url.contains("/front/wa/yaoqinghaoyou")) {
						new ShareAction(YaoqingWeb.this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
								.withTitle(getString(R.string.weixin_titl))
								.withMedia(image)
								.withText(getString(R.string.weixin_text))
								.withTargetUrl(appUrl)
								.share();
					} else if (url.contains("/front/wa/pengyouquan")) {
						new ShareAction(YaoqingWeb.this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
								.withMedia(image)
								.withTitle(getString(R.string.pengyouquan_titl))
								.withText(getString(R.string.pengyouquan_text))
								.withTargetUrl(appUrl)
								.share();
						Log.i("appUrl", appUrl);
					} else {
						view.loadUrl(url);
					}
				}
				return true;
			}
		});
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setDefaultTextEncodingName("gb2312");
		mWebView.loadUrl(url);
	}

	private UMShareListener umShareListener = new UMShareListener() {
		@Override
		public void onResult(SHARE_MEDIA platform) {
			com.umeng.socialize.utils.Log.d("plat", "platform" + platform);
			Toast.makeText(YaoqingWeb.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onError(SHARE_MEDIA platform, Throwable t) {
			Toast.makeText(YaoqingWeb.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onCancel(SHARE_MEDIA platform) {
			Toast.makeText(YaoqingWeb.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWebView = null;

	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK ||event.getAction() == KeyEvent.ACTION_DOWN) {
			if (mWebView.canGoBack()) {
				mWebView.goBack();// 返回前一个页面
			}else{
				this.finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
